<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace app\common\model;

use app\common\lib\exception\ApiException;
use think\Cache;

/**
 * 文章点赞模型
 * Class ArticleGood
 * @package app\common\mode
 */
class ArticleCategory extends Base
{
    protected $table = 'wechatadmin.system_category';

    const CATEGORY = 'category';//星文分类缓存键名
    const CACHE_TIME = 86400*15;


    /**
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @throws ApiException
     */
    public function getArticleCategory()
    {
        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get(self::CATEGORY);
        }
        if ($data === false)
        {
            try
            {
                $field = 'id,name';

                $data = $this->field($field)->field('id,name')
                    ->where('status=1 and (id < 3 or id > 42)')
                    ->order('sort DESC')
                    ->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set(self::CATEGORY, $data, self::CACHE_TIME);
            }
            catch (\Exception $e)
            {
                \think\Log::write('文章栏目获取失败：' . $e->getMessage(), 'category_error', true);

                throw new ApiException('文章栏目获取失败');
            }
        }
        return $data;
    }
}
